package com.central.gateway.monitor.config;

import com.alibaba.nacos.common.utils.HttpMethod;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

/**
 * @author wdd
 * @date 2020/10/21
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                //允许所有静态文件访问
                .antMatchers(this.adminContextPath + "/assets/**").permitAll()
                //允许登陆页面访问
                .antMatchers(this.adminContextPath + "/login").permitAll()
                //允许admin监控访问
                .antMatchers(this.adminContextPath + "/instances").permitAll()
                .antMatchers(this.adminContextPath + "/instances/*").permitAll()
                .antMatchers(this.adminContextPath + "/actuator/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage(this.adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(this.adminContextPath + "/logout");

    }
}
